package com.dingwei.superiordiary.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.NewSuperiorProductsAdapter;
import com.dingwei.superiordiary.bean.GoodsListBean;
import com.dingwei.superiordiary.bean.Index_Bean;
import com.dingwei.superiordiary.mvp.presenter.RankingListFragmentPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.CommodityDetailsActivity;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.L;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/4.
 * 首页-精选
 */

public class RankingListFragment extends LazyLoadFragment implements Contract.RankingListFragmentView {
    RankingListFragmentPresenter rankingListFragmentPresenter = new RankingListFragmentPresenter(this);
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private NewSuperiorProductsAdapter mAdapter;
    private String order;
    private String category_id;
    private int page = 1;

    //*****************主要代码********************
    //接收数据
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        category_id = getArguments().getString("id");
        order = getArguments().getString("order");
        L.e("id = " + getArguments().getString("id"));
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_ranking_list;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }


    private void initView() {


        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new NewSuperiorProductsAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(mAdapter);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                rankingListFragmentPresenter.initData(page, category_id, order, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                rankingListFragmentPresenter.initData(page, category_id, order, true, false);
            }
        });
        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                intent  = new Intent(BaseActivity.context, CommodityDetailsActivity.class);
                GoodsListBean.DataBean bean = mAdapter.getItem(position);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    protected void lazyLoad() {
        refreshLayout.autoRefresh();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onFail() {
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
        showData("fail");
    }

    @Override
    public void onLoadMore(GoodsListBean bean) {
        mAdapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onRefresh(GoodsListBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (mAdapter.getAllData() != null && mAdapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.no_net_btn)
    public void onViewClicked() {
        page = 1;
        rankingListFragmentPresenter.initData(page, category_id, order, true, true);
    }
}
